package com.taozitongxue.string;

import java.util.ArrayList;

class User {
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

public class StringSecurityExample {
    public static void main(String[] args) {
        String username = "沉默王二";
        String password = "123456";
        User user = new User(username, password);

        // 获取用户凭据
        String[] credentials = getUserCredentials(user);

        // 尝试修改从 getUserCredentials 返回的用户名和密码字符串
        credentials[0] = "陈清扬";
        credentials[1] = "612311";

        // 输出原始 User 对象中的用户名和密码
        System.out.println("原始用户名: " + user.getUsername());
        System.out.println("原始密码: " + user.getPassword());
        System.out.println("用户名:"+credentials[0]);
        System.out.println("密码:"+credentials[1]);

        StringBuilder sb = new StringBuilder("A");
        modifyRef(sb);
        System.out.println(sb); // 输出"A"

        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        modArrays(arrayList);
        for (Integer integer : arrayList) {
            System.out.println(integer);
        }
    }

    public static String[] getUserCredentials(User user) {
        String[] credentials = new String[2];
        credentials[0] = user.getUsername();
        credentials[1] = user.getPassword();
        return credentials;
    }
    static void modifyRef(StringBuilder s) {
//        s = new StringBuilder("B"); // 地址副本指向新对象
        s.append("b");
    }
    public static void modArrays(ArrayList<Integer> arrayList){
        arrayList.set(0,100);
        arrayList.set(1,101);
        arrayList.add(102);
    }
}